﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data;
using MySql.Data.MySqlClient;

/// <summary>
/// Descripción breve de FilialesDA
/// </summary>
public static class FilialesDA
{
    public static string insertFilial(string filialid, string filDesc, string filDirecc, string filLocalidad, string filCodPost)
    {
        string consulta;
        MySqlConnection conexion = Conexion.obtConexion();
        consulta = @"INSERT INTO filiales
                    VALUES ('" + filialid + "','" + filDesc + "','" + filDirecc + "','" + filLocalidad + "','" + filCodPost + "')";
        MySqlCommand comando = new MySqlCommand(consulta, conexion);
        try
        {
            conexion.Open();
            comando.ExecuteReader();
            conexion.Close();
            return "ok";
        }
        catch
        {
            return consulta;
        }
    }
    
    public static DataTable selectFilial(string filId)
    {
        MySqlConnection conexion = Conexion.obtConexion();
        DataTable tabla = new DataTable();
        string consulta = "SELECT * FROM filiales WHERE filialid=" + filId;
        MySqlCommand comando = new MySqlCommand(consulta, conexion);
        conexion.Open();
        MySqlDataReader dataReader = comando.ExecuteReader();
        tabla.Load(dataReader, LoadOption.OverwriteChanges);
        conexion.Close();
        return tabla;
    }

    public static DataTable selectFiliales()
    {
        MySqlConnection conexion = Conexion.obtConexion();
        DataTable tabla = new DataTable();
        string consulta = @"SELECT `filiales`.`filialid` AS `ID`, `filiales`.`filiallocalidad` AS `Localidad`
                            FROM `filiales`";
        MySqlCommand comando = new MySqlCommand(consulta, conexion);
        conexion.Open();
        MySqlDataReader dataReader = comando.ExecuteReader();
        tabla.Load(dataReader, LoadOption.OverwriteChanges);
        conexion.Close();
        return tabla;
    }

    public static string deleteFilial(string filialid)
    {
        MySqlConnection conexion = Conexion.obtConexion();
        string consulta = @"DELETE FROM filiales
                            WHERE filialid=" + filialid;
        MySqlCommand comando = new MySqlCommand(consulta, conexion);
        try
        {
            conexion.Open();
            comando.ExecuteReader();
            conexion.Close();
            return "ok";
        }
        catch
        {
            return consulta;
        }
    }

    public static string updateFilial(string filialid, string filialdesc, string filialdireccion, string filiallocalidad, string filialcodpostal)
    {
        MySqlConnection conexion = Conexion.obtConexion();
        DataTable tabla = new DataTable();
        string consulta = @"UPDATE filiales
                            SET filialdesc='" + filialdesc +
                            "', filialdireccion='" + filialdireccion +
                            "', filiallocalidad='" + filiallocalidad +
                            "', filialcodpostal='" + filialcodpostal +
                            "' WHERE filialid=" + filialid;
        MySqlCommand comando = new MySqlCommand(consulta, conexion);
        try
        {
            conexion.Open();
            comando.ExecuteReader();
            conexion.Close();
            return "ok";
        }
        catch
        {
            return consulta;
        }
    }
}
